package com.hyny.framework.platform.system.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hyny.framework.platform.system.entity.dto.FieldPowerDTO;
import com.hyny.framework.platform.system.entity.pojo.SysFieldPower;
import com.hyny.framework.platform.system.service.SysFieldPowerService;
import com.hyny.framework.platform.system.utils.FieldPowerUtil;
import com.hyny.framework.platform.system.utils.bean.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "工具")
@ApiSupport(order=9)
@RestController
@Slf4j
public class ToolController {
    //业务请求地址
    @Value("${hyny.knife4j.basePackageBiz}")
    private String basePackageBiz = "";
    @Autowired
    private SysFieldPowerService sysFieldPowerService;
    public Result  info(){
        /**
         * TODO 做一个工具方法列表，返回一个集合，包括工具名称，请求地址，动态表单等信息
         *      用于列表展示 ，页面根据列表点开，展示出动态表单，点击确定，执行动态请求地址，下面该请求的其他方法
         */

        return null;
    }

    @ApiOperation(value = "刷新字段权限",notes = "刷新字段权限")
    @PostMapping("/refreshFieldPower")
    public Result RefreshFieldPower(){
        List<FieldPowerDTO> fieldPowerDTOList= FieldPowerUtil.packageFieldPower(basePackageBiz,"BIZ");
        //遍历项目下需要配置权限类
        for (FieldPowerDTO classFieldPowerDTO : fieldPowerDTOList) {
            //添加类权限配置
            SysFieldPower classSysFieldPower = saveUpdateFieldPower(classFieldPowerDTO, null);
            //遍历类下方法权限配置信息
            for (FieldPowerDTO methodFieldPowerDTO : classFieldPowerDTO.getFieldPowerDTOList()) {
                //添加方法权限配置
                SysFieldPower methodSysFieldPower = saveUpdateFieldPower(methodFieldPowerDTO, classSysFieldPower);
                //遍历方法返回字段权限配置
                for (FieldPowerDTO fieldFieldPowerDTO : methodFieldPowerDTO.getFieldPowerDTOList()) {
                    //添加字段权限配置
                    saveUpdateFieldPower(fieldFieldPowerDTO, methodSysFieldPower);
                }
            }
        }
        return Result.ok();
    }
    /**
     * 添加修改字段权限配置信息
     * @param fieldPowerDTO
     * @param sysFieldPower
     * @return
     */
    public SysFieldPower saveUpdateFieldPower(FieldPowerDTO fieldPowerDTO, SysFieldPower sysFieldPower) {
        LambdaQueryWrapper<SysFieldPower> fieldPowerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fieldPowerLambdaQueryWrapper.eq(SysFieldPower::getPermissions, fieldPowerDTO.getPermissions());
        fieldPowerLambdaQueryWrapper.eq(SysFieldPower::getPosition, fieldPowerDTO.getPosition());
        if(sysFieldPower != null&&sysFieldPower.getId()!=null){
            fieldPowerLambdaQueryWrapper.eq(SysFieldPower::getParentId, sysFieldPower.getId());
        }
        //数据转换
        SysFieldPower newSysFieldPower = sysFieldPowerService.getOne(fieldPowerLambdaQueryWrapper);
        if(newSysFieldPower==null){
            newSysFieldPower=new SysFieldPower();
        }
        newSysFieldPower.setParentId(sysFieldPower != null&&sysFieldPower.getId() != null?sysFieldPower.getId():0L);
        newSysFieldPower.setTitle(fieldPowerDTO.getTitle());
        newSysFieldPower.setType(fieldPowerDTO.getType());
        newSysFieldPower.setPosition(fieldPowerDTO.getPosition());
        newSysFieldPower.setClassName(fieldPowerDTO.getClassName());
        newSysFieldPower.setClassTitle(sysFieldPower!=null&& StrUtil.isNotBlank(sysFieldPower.getClassTitle())?sysFieldPower.getClassTitle():StrUtil.isNotBlank(fieldPowerDTO.getClassName())?fieldPowerDTO.getTitle():"");
        newSysFieldPower.setMethodName(fieldPowerDTO.getMethodName());
        newSysFieldPower.setMethodTitle(sysFieldPower!=null&&StrUtil.isNotBlank(sysFieldPower.getMethodTitle())?sysFieldPower.getMethodTitle():StrUtil.isNotBlank(fieldPowerDTO.getMethodName())?fieldPowerDTO.getTitle():"");
        newSysFieldPower.setFieldName(fieldPowerDTO.getFieldName());
        newSysFieldPower.setFieldTitle(sysFieldPower!=null&&StrUtil.isNotBlank(sysFieldPower.getFieldTitle())?sysFieldPower.getFieldTitle():StrUtil.isNotBlank(fieldPowerDTO.getFieldName())?fieldPowerDTO.getTitle():"");
        newSysFieldPower.setPermissions(fieldPowerDTO.getPermissions());
        sysFieldPowerService.saveOrUpdate(newSysFieldPower);
        log.info("保存实体：{}",JSONObject.toJSONString(newSysFieldPower));
        return newSysFieldPower;
    }
}
